﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MedicalCare.Messages;
using StructureMap;
using MedicalCare.DbLayer.Adaptors;
using System.Text;
using MedicalCare.EntityDefinitions.Entities;

namespace MedicalCare.Admin
{
    public partial class AdminScheduleSearch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 按条件搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSearchOnClick(object sender, EventArgs e)
        {
            var message = new ScheduleMessage()
            {
                FamilyName = TxbPatient.Text,
                NurseName = TxbNurse.Text,
                StartDT = !string.IsNullOrEmpty(TxbBindStarDate.Text) ? Convert.ToDateTime(TxbBindStarDate.Text) :new DateTime(1990,1,1),
                EndDT = !string.IsNullOrEmpty(TxbBindEndDate.Text)? Convert.ToDateTime(TxbBindEndDate.Text):new DateTime(1990,1,1)
            };

            var scheduleAdaptor = ObjectFactory.GetInstance<IScheduleAdaptor>();

            StringBuilder whereString = new StringBuilder(" 0=0");
            if (!string.IsNullOrEmpty(message.FamilyName))
            {
                whereString.AppendFormat(" and fam.Name like '%{0}%'", message.FamilyName);
            }
            if (!string.IsNullOrEmpty(message.NurseName))
            {
                whereString.AppendFormat(" and sche.NurseId={0}",Common.User.GetUserIDByUserName(message.NurseName));
            }
            if (message.StartDT.Date.Year > 1990)
            {
                whereString.AppendFormat(" and sche.StartDT>='{0}'",message.StartDT.ToString("yyyy-MM-dd"));
            }

            if (message.EndDT.Date.Year > 1990)
            {
                whereString.AppendFormat(" and sche.EndDT<'{0}'",message.EndDT.AddDays(1).ToString("yyyy-MM-dd"));
            }

            ObjectDataSource1.SelectParameters["orderKey"].DefaultValue = "sche.StartDT";
            ObjectDataSource1.SelectParameters["orderVal"].DefaultValue = "Desc";
            ObjectDataSource1.SelectParameters["whereStr"].DefaultValue = whereString.ToString();

            //var scheduleEntityList = scheduleAdaptor.GetSchedule("sche.StartDT", "Desc", whereString.ToString());
            //if (scheduleEntityList!=null && scheduleEntityList.Count > 0)
            //{
            //    GridView1.Visible = true;
            //    GridView1.DataSource = new PagedDataSource(){DataSource =  scheduleEntityList};
            //    GridView1.DataBind();
            //}
            //else
            //{
            //    GridView1.Visible = false;
            //    //GridView1.DataSource = null;
            //   // GridView1.DataBind();
            //}
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var grid = (GridView)sender;
                var lable = (System.Web.UI.WebControls.Label)e.Row.Cells[0].FindControl("lblNo");
                lable.Text = (grid.PageIndex * grid.PageSize + e.Row.RowIndex + 1).ToString();

                var entity = (ScheduleEntity)e.Row.DataItem;
            }
        }
    }
}